package be.jaisy.osgi.console.ui.client.osgiservice;

import java.util.Collection;
import java.util.Iterator;

import be.jaisy.osgi.console.service.api.IOsgiServiceManagementService;
import be.jaisy.osgi.console.service.api.IOsgiServiceManagementServiceAsync;
import be.jaisy.osgi.console.service.api.to.OsgiServiceTo;
import be.jaisy.osgi.console.ui.client.OsgiConsole;
import be.jaisy.ui.gwt.client.util.ConvertionHelper;
import be.jaisy.ui.gwt.client.widget.Sink;
import be.jaisy.ui.gwt.client.widget.table.Table;
import be.jaisy.ui.gwt.client.widget.table.popup.Popup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TreeImages;
import com.google.gwt.user.client.ui.Widget;

/**
 * Demonstrates the various button widgets.
 */
public class OsgiService extends Sink {

	public static SinkInfo init() {
		return new SinkInfo("OsgiService", "<p>Control registered Osgi Service.</p>") {

			public Sink createInstance() {
				return new OsgiService();
			}

			public String getColor() {
				return "#fe9915";
			}
		};
	}

	/**
	 * An image provider to make available images to Bundle.
	 */
	public interface Images extends ImageBundle, TreeImages {
		AbstractImagePrototype info();
	}

	private final IOsgiServiceManagementServiceAsync service;

	private final Table table;

	private static final String[] headers = new String[] { "Id", "Interfaces", "", "Ranking", "Vendor", "Description" };

	public OsgiService() {

		// Initialize the service.
		service = (IOsgiServiceManagementServiceAsync) GWT.create(IOsgiServiceManagementService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget) service;
		endpoint.setServiceEntryPoint(OsgiConsole.MODULE_RELATIVE_PATH);

		Widget[] filters = new Widget[6];
		filters[1] = new Label(){

			public void onBrowserEvent(Event event) {
				refreshData();
				super.onBrowserEvent(event);
			}
			
		};
		
		table = new Table(headers, filters); 
		refreshData();
		initWidget(table);
	}

	public void onShow() {
		refreshData();
	}

	private void refreshData() {
		service.listRegisteredServices(new AsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			public void onSuccess(Object result) {

				table.clearData();
				Collection osgiServiceTos = (Collection) result;
				int row = 2;
				Iterator iter = osgiServiceTos.iterator();
				while (iter.hasNext()) {
					final OsgiServiceTo osgiServiceTo = (OsgiServiceTo) iter.next();
					table.setValue(row, 0, ConvertionHelper.convert(osgiServiceTo.getId()));
					table.setValue(row, 1, ConvertionHelper.convert(osgiServiceTo.getInterfaceClassNames()));
					Image image =  ((Images) GWT.create(Images.class)).info().createImage();
					image.addClickListener(new ClickListener() {

						public void onClick(Widget sender) {
							Grid grid = new Grid(7, 2);
							grid.setText(0, 0, "id");
							grid.setText(0, 1, ConvertionHelper.convert(osgiServiceTo.getId()) );
							grid.setText(1, 0, "interfaces");
							grid.setText(1, 1, ConvertionHelper.convert(osgiServiceTo.getInterfaceClassNames()));
							grid.setText(2, 0, "ranking");
							grid.setText(2, 1, ConvertionHelper.convert(osgiServiceTo.getRanking()) );
							grid.setText(3, 0, "vendor");
							grid.setText(3, 1,  osgiServiceTo.getVendor() );
							grid.setText(4, 0, "description");
							grid.setText(4, 1, osgiServiceTo.getDescription() );
							grid.setText(5, 0, "registeredBundleId");
							grid.setText(5, 1,  ConvertionHelper.convert(osgiServiceTo.getRegisteredBundleid()));
							grid.setText(6, 0, "usedByBundleIds");
							grid.setText(6, 1, ConvertionHelper.convert(osgiServiceTo.getUsedByBundleIds()));
							Popup p = new Popup(grid);
							int left = sender.getAbsoluteLeft() + 10;
							int top = sender.getAbsoluteTop() + 10;
							p.setPopupPosition(left, top);
							p.show();
						}

					});
					table.setWidget(row, 2, image);
					table.setValue(row, 3, ConvertionHelper.convert(osgiServiceTo.getRanking()));
					table.setValue(row, 4, osgiServiceTo.getVendor());
					table.setValue(row, 5, osgiServiceTo.getDescription());
					row++;
				}
			}
		});
	}
}
